#!usr/bin/env python
#-*- coding:utf-8 -*-
"""
数据源：巨潮 -> 市场资讯 -> 上市公司 -> 公司资讯 -> 分红配股
爬取类目：配股(不包含配股)
股票类型不包括港股（港股没有最新资料）
url_sample: http://www.cninfo.com.cn/information/allotment/szmb000001.html
"""
import sys
import json
from xtls.logger import get_logger
from company_formaltable_crawler import CompanyFormalTable


reload(sys)
sys.setdefaultencoding('utf-8')
"""
result_sample_of_company_allotment = {
    '_id': '',
    'stockCode': '',
    'companyShortName': '',
    'stockType': '',
    'allotment': [{
        'allotYear': '',
        'allotPlan': '',
        'allotPrice': '',
        'recordDate': '',
        'rightOutDate': '',
        'startDate': '',
        'stopDate': '',
        'circulateOfferDate': '',
    },],
}
"""


def get_detail_mapping():
    DETAIL_MAPPING = {
        u'配股年度': 'allotYear',
        u'配股方案': 'allotPlan',
        u'配股价': 'allotPrice',
        u'股权登记日': 'recordDate',
        u'除权基准日'	: 'rightOutDate',
        u'配股交款起止日': 'startStopDate',
        u'配股可流通部分上市日': 'circulateOfferDate',
    }
    return DETAIL_MAPPING


class CompanyAllotment(CompanyFormalTable):
    def __init__(self, stock_list):
        logger = get_logger(__file__)
        super(CompanyAllotment, self).__init__(stock_list=stock_list, logger=logger)
        self.category = 'allotment'
        self.tab_name = 'allotment'
        self.detail_mapping = get_detail_mapping()

    def clean_data(self, dirty_data):
        start_stop_date = dirty_data.pop('startStopDate')
        dirty_data['startDate'] = start_stop_date.split('-')[0].strip() if start_stop_date else None
        dirty_data['stopDate'] = start_stop_date.split('-')[1].strip() if start_stop_date else None
        self.cleaner.set_dirty_data(dirty_data)
        return self.cleaner.complete()  # 对dirty_data中的每个数据去开头和结尾空格, 如果是空str就返回None

    def save_update(self, data):
        allotment_list = data.get('allotment')
        if not allotment_list:
            self.logger.warn('allotment has no information %s-%s' % (data.get('stock_id'), data.get('stock_code')))
            return
        if not data.get('stock_id'):
            return
        
        version_num = 1 + self.select_current_version_num(table_name='allotment', stock_id=data.get('stock_id'))
        for single in allotment_list:
            single_clean = self.clean_data(single)
            single_clean['stockId'] = data.get('stock_id', None)
            single_clean['versionNum'] = version_num

            self.insert_to_db(table_name='allotment', cleaned_data=single_clean)

    def save_print(self, data):
        print json.dumps(data)
        print "=" * 70

def main():
    pass

if __name__ == '__main__':
    main()
